/*
 * Copyright (C) 2009 Kristopher T Babic
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses>.
 */

package edu.idp.client;

import gjt.Util;
import edu.idp.client.event.ServerStatusEvent;
import edu.idp.client.interfaces.ServerStatusListener;

import java.applet.Applet;
import java.awt.*;
import java.net.URL;
import java.util.Hashtable;

/**
 *
 * @author Kristopher T Babic
 */
public class ServerStatusPanel extends Panel implements ServerStatusListener {
//  Image serverUp = null;
//  Image serverDown = null;

    Image current = null;
    // String currentString = null;
//  Image a,b,c,d,e,f,g,h;

    Hashtable images = new Hashtable();

    // new stuff here per request
    // Hashtable statusString = new Hashtable();
    String currentStr = "Server status is unknown";

    public ServerStatusPanel(Applet ap) {
/*
    Applet ap = Util.getApplet(this);
    if (ap == null) {
      serverUp = this.getToolkit().getImage("./gif/)
  */
//  System.out.println("in server status\n");
        //Toolkit tk = Toolkit.getDefaultToolkit();
        Applet tk = ap;//Util.getApplet(this);
        URL base = tk.getCodeBase();
//  System.out.println("in server status\n");
        Image a = tk.getImage(base, "./gifs/net_conn.gif");
        Image b = tk.getImage(base, "./gifs/net_disc.gif");
        Image c = tk.getImage(base, "./gifs/net_error.gif");
        Image d = tk.getImage(base, "./gifs/net_receive.gif");
        Image e = tk.getImage(base, "./gifs/net_send.gif");
        Image f = tk.getImage(base, "./gifs/net_connecting.gif");
        Image g = tk.getImage(base, "./gifs/net_unknown.gif");
        Image h = tk.getImage(base, "./gifs/net_lost.gif");
///  System.out.println("in server status\n");
        Util.waitForImage(this, a);
        Util.waitForImage(this, b);
        Util.waitForImage(this, c);
        Util.waitForImage(this, d);
        Util.waitForImage(this, e);
        Util.waitForImage(this, f);
        Util.waitForImage(this, g);
        Util.waitForImage(this, h);
//  System.out.println("in server status\n");

        images.put(new Integer(ServerStatusEvent.CONNECTED), a);
        images.put(new Integer(ServerStatusEvent.DISCONNECTED), b);
        images.put(new Integer(ServerStatusEvent.SERVER_ERROR), c);
        images.put(new Integer(ServerStatusEvent.RECEIVING_DATA), d);
        images.put(new Integer(ServerStatusEvent.SENDING_DATA), e);
        images.put(new Integer(ServerStatusEvent.CONNECTING), f);
        images.put(new Integer(ServerStatusEvent.UNKNOWN), g);
        images.put(new Integer(ServerStatusEvent.SERVER_LOST), h);
        /*
         images.put(new Integer(ServerStatusEvent.CONNECTED),"Connected to Server.");
         images.put(new Integer(ServerStatusEvent.DISCONNECTED),"Not Connected to Server");
         images.put(new Integer(ServerStatusEvent.SERVER_ERROR),"Error Occurred on Server Side");
         images.put(new Integer(ServerStatusEvent.RECEIVING_DATA),"Receiving Data from Server");
         images.put(new Integer(ServerStatusEvent.SENDING_DATA),"Sending Data to Server");
         images.put(new Integer(ServerStatusEvent.CONNECTING),"Establishing Connection to Server");
         images.put(new Integer(ServerStatusEvent.UNKNOWN),"Connection Status Unknown");
         images.put(new Integer(ServerStatusEvent.SERVER_LOST),"Server Connection Has Been Lost");
        */
        current = g;

/*
    if (a == null ||
      b == null ||
      c == null ||
      d == null ||
      e == null ||
      f == null ||
      g == null)
        System.out.println ("bull shit");
 */
    }

    public void handleServerStatusEvent(ServerStatusEvent sse) {
//    currentStr = sse.toString();
        current = (Image) images.get(new Integer(sse.getStatus()));
        repaint();
    }

    public void paint(Graphics g) {
//               if (current == null)
//                System.out.println("what the fuck");
//               else
//                System.out.println("in paint");
//    g.drawString(currentStr, 10, this.getY()/2);
//System.out.println(a);
//System.out.println(a.getWidth(this)+" "+a.getHeight(this));
//    g.drawString(currentString, 0, 20);
        g.drawImage(current, 0, 0, 100, 50, this);

//    g.drawLine(0,0,200, 20);
    }
}